//
//  Config.m
//  myhuibao
//
//  Created by li jun on 13-3-5.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "Config.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Config
-(void)saveLogin:(NSDictionary *)member
{
    int memberId = [[member objectForKey:@"memberID"] intValue];

    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"memberId"];
    [settings removeObjectForKey:@"password"];
    [settings removeObjectForKey:@"name"];
    [settings removeObjectForKey:@"nickName"];
    [settings removeObjectForKey:@"mobile"];
    [settings removeObjectForKey:@"levelName"];
    [settings removeObjectForKey:@"payPassword"];
    [settings removeObjectForKey:@"filePath"];
    [settings removeObjectForKey:@"fileName"];
    [settings removeObjectForKey:@"LoginPass"];
    [settings setObject:[NSString stringWithFormat:@"%d", memberId] forKey:@"memberId"];
    [settings setObject:[member objectForKey:@"password"] forKey:@"password"];
    [settings setObject:[member objectForKey:@"name"] forKey:@"name"];
    [settings setObject:[member objectForKey:@"nickName"] forKey:@"nickName"];
    [settings setObject:[member objectForKey:@"mobile"] forKey:@"mobile"];
    [settings setObject:[member objectForKey:@"levelName"] forKey:@"levelName"];
    [settings setObject:[member objectForKey:@"payPassword"] forKey:@"payPassword"];
     [settings setObject:[member objectForKey:@"filePath"] forKey:@"filePath"];
     [settings setObject:[member objectForKey:@"fileName"] forKey:@"fileName"];
    [settings setObject:[member objectForKey:@"LoginPass"] forKey:@"LoginPass"];
    [settings synchronize];
}

-(NSString*)getLoginPass
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"LoginPass"];
}

-(NSString *)getName
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"name"];
}

-(NSString *)getPassword
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    NSString * temp = [settings objectForKey:@"password"];
    return temp;
//    return [AESCrypt decrypt:temp password:@"pwd"];
}
-(NSString *)getNickName
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"nickName"];
}

-(NSString *)getMobile
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"mobile"];
}

-(NSString *)getLevelName
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"levelName"];
}

-(NSString *)getPayPassword
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"payPassword"];
}

//-(void)saveUID:(int)uid
//{
//    NSUserDefaults *setting = [NSUserDefaults standardUserDefaults];
//    [setting removeObjectForKey:@"UID"];
//    [setting setObject:[NSString stringWithFormat:@"%d", uid] forKey:@"UID"];
//    [setting synchronize];
//}
-(int)getMemberId
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"memberId"];
//    NSLog(@"%@",value);

    if (value && [value isEqualToString:@""] == NO)
    {
        return [value intValue];
    }
    else
    {
        return 0;
    }
}

-(NSString *)getCity
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"city"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return value;
    }else
    {
        return @"合肥";
    }
}
-(NSString *)getLocateCity
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"locatecity"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return value;
    }else
    {
        return @"未知";
    }
}
-(NSString *)getLocation
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    return [settings objectForKey:@"location"];
}

-(NSString*) getImageUrl
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    if([[settings objectForKey:@"filePath"] length]==0 || [[settings objectForKey:@"fileName"] length]==0)
    {
        return @"";
    }
    NSString *str=[NSString stringWithFormat:@"%@%@%@",image_url,[settings objectForKey:@"filePath"],[settings objectForKey:@"fileName"]];
    return str;
}

-(int)getCityId
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"cityId"];    
    if (value && [value isEqualToString:@""] == NO)
    {
        return [value intValue];
    }
    else
    {
        return 1;//默认城市id合肥
    }
}
-(int)getFirstRun
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"firstRun"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return [value intValue];
    }
    else
    {
        return 0;//默认
    }
}
-(bool)getChecked
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"checked"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return [value boolValue];
    }
    else
    {
        return false;//默认
    }
}
-(float)getLatitude
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"latitude"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return [value floatValue];
    }
    else
    {
        return 0;//默认
    }
}
-(float)getLongitude
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString *value = [setting objectForKey:@"longitude"];
    if (value && [value isEqualToString:@""] == NO)
    {
        return [value floatValue];
    }
    else
    {
        return 0;//默认
    }
}
-(void)setMemberId:(int) memberId {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"memberId"];
    [settings setObject:[NSString stringWithFormat:@"%d",memberId] forKey:@"memberId"];
    [settings synchronize];
}

-(void)setName:(NSString *)name {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"name"];
    [settings setObject:name forKey:@"name"];
    [settings synchronize];
}

-(void)setPhone:(NSString *)phone {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"mobile"];
    [settings setObject:phone forKey:@"mobile"];
    [settings synchronize];
}

-(void)setPassword:(NSString *)password
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"password"];
    [settings setObject:password forKey:@"password"];[settings synchronize];
}
-(void)setNickName:(NSString *)nickName
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"nickName"];
    [settings setObject:nickName forKey:@"nickName"];[settings synchronize];
}

-(void)setPayPassword:(NSString *)payPassword
{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"payPassword"];
    [settings setObject:payPassword forKey:@"payPassword"];[settings synchronize];
}
-(void)setCity:(NSString *)city{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"city"];
    [settings setObject:city forKey:@"city"];
    [settings synchronize];
}
-(void)setLocateCity:(NSString *)city{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"locatecity"];
    [settings setObject:city forKey:@"locatecity"];
    [settings synchronize];
}
-(void)setLocation:(NSString *)location {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"location"];
    [settings setObject:location forKey:@"location"];
    [settings synchronize];
}
-(void)setCityId:(int)cityId{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"cityId"];
    [settings setObject:[NSString stringWithFormat:@"%d",cityId] forKey:@"cityId"];
    [settings synchronize];
}
-(void)setLatitude:(float)latitude {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"latitude"];
    [settings setObject:[NSString stringWithFormat:@"%f",latitude] forKey:@"latitude"];
    [settings synchronize];
}
-(void)setLongitude:(float)longitude {
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"longitude"];
    [settings setObject:[NSString stringWithFormat:@"%f",longitude] forKey:@"longitude"];
    [settings synchronize];
}
-(void)setFirstRun:(int)flag{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"firstRun"];
    [settings setObject:[NSString stringWithFormat:@"%d",flag] forKey:@"firstRun"];
    [settings synchronize];
}
-(void)setChecked:(bool)flag{
    NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:@"checked"];
    [settings setObject:[NSString stringWithFormat:@"%d",flag] forKey:@"checked"];
    [settings synchronize];
}
#pragma 单例模式
static Config * instance = nil;
+(Config *) Instance
{
    @synchronized(self)
    {
        if(nil == instance)
        {
            [self new];
        }
    }
    return instance;
}
+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if(instance == nil)
        {
            instance = [super allocWithZone:zone];
            return instance;
        }
    }
    return nil;
}
// 正则判断手机号码地址格式
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,183
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0235-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
//删除空白cell
+ (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

//MD5加密
- (NSString *)md5HexDigest:(NSString*)password
{
    const char *original_str = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
    {
        [hash appendFormat:@"%02X", result[i]];
    }
    NSString *mdfiveString = [hash lowercaseString];
    NSLog(@"Encryption Result = %@",mdfiveString);
    return mdfiveString;
}

@end
